package com.xdemo.community.builder;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import com.xdemo.community.handler.ServerMessageAction;
import com.xdemo.community.server.CommunityServerConfig;
import com.xdemo.community.socketserver.CommunitySocketServerHandler;
import com.xdemo.handler.MessageActionRouter;
import com.xdemo.handler.TestExchangeAction;
import com.xdemo.manager.AsyncManagerImpl;
import com.xdemo.manager.DBManager;
import com.xdemo.manager.IAsyncManager;
import com.xdemo.model.VirtualUser;
import com.xdemo.processor.IProcessor;
import com.xdemo.processor.QueueProcessor;

public class Builder {

	public static MessageActionRouter<Integer> buildMessageRouter() {
		MessageActionRouter<Integer> _router = new MessageActionRouter<Integer>();
		_router.registerAction(new TestExchangeAction());
		_router.registerAction(new ServerMessageAction());
		return _router;
	}
	
	public static IProcessor<Integer> buildProcessor(MessageActionRouter<Integer> router) {
		IProcessor<Integer> _processor = new QueueProcessor<Integer>(router);
		return _processor;
	}
	
	public static CommunitySocketServerHandler buildServerHandler(IProcessor<Integer> processor) {
		CommunitySocketServerHandler _handler = new CommunitySocketServerHandler(processor);
		return _handler;
	}
	
	public static IAsyncManager buildAsyncManager(IProcessor<Integer> processor,CommunityServerConfig serverConfig) {
		Executor _executor = Executors.newFixedThreadPool(serverConfig.getAsyncThreadsNum());
		IAsyncManager _manager = new AsyncManagerImpl(processor,_executor);
		return _manager;
	}
	
	public static DBManager buildDBManager() {
		Configuration _config = new AnnotationConfiguration()
									.addAnnotatedClass(VirtualUser.class);
		SessionFactory _sessionFactory = _config.buildSessionFactory();
		DBManager _db = new DBManager(_sessionFactory);
		return _db;
	}
	
}
